package com.dubious.salesforce;

import static java.util.Arrays.asList;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class ComponentReader {

    public enum Command {
        DEPEND, INSTALL, REMOVE, LIST, END;
    }

    public void processFile(String fileName) throws Exception {
        ComponentInstaller componentInstaller = new ComponentInstaller();

        // read file line by line
        BufferedReader reader =
                new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
        String line = reader.readLine();
        while (!line.trim().equals(Command.END.toString())) {
            // echo
            System.out.println(line);

            // break up the input into parts using spaces as the divider
            List<String> lineParts = asList(line.split(" "));
            List<String> betterLineParts = new ArrayList<String>();
            for (String linePart : lineParts) {
                if (linePart.trim().length() > 0) {
                    betterLineParts.add(linePart);
                }
            }
            lineParts = betterLineParts;

            // treat line based on first part - the command
            String command = lineParts.get(0);
            try {
                if (command.equals(Command.DEPEND.toString())) {
                    depend(
                            componentInstaller,
                            lineParts.get(1),
                            lineParts.subList(2, lineParts.size()));
                } else if (command.equals(Command.INSTALL.toString())) {
                    install(componentInstaller, lineParts.get(1));
                } else if (command.equals(Command.REMOVE.toString())) {
                    remove(componentInstaller, lineParts.get(1));
                } else if (command.equals(Command.LIST.toString())) {
                    list(componentInstaller);
                } else {
                    System.out.println("   Do not recognize command.");
                }
            } catch (Exception e) {
                // error. output the error message
                System.out.println("   " + e.getMessage());
            }

            line = reader.readLine();
        }

        System.out.println("END");
    }

    private void depend(
            ComponentInstaller componentInstaller,
            String parent,
            List<String> children) throws Exception {
        componentInstaller.depend(parent, children);
    }

    private void install(ComponentInstaller componentInstaller, String nodeName)
            throws Exception {
        List<String> installedNodes = componentInstaller.install(nodeName);
        for (String installedNode : installedNodes) {
            System.out.println("   Installing " + installedNode);
        }
    }

    private void remove(ComponentInstaller componentInstaller, String nodeName) throws Exception {
        List<String> removedNodes = componentInstaller.remove(nodeName);
        for (String removedNode : removedNodes) {
            System.out.println("   Removing " + removedNode);
        }
    }

    private void list(ComponentInstaller componentInstaller) {
        Set<String> installedNodes = componentInstaller.list();
        for (String installedNode : installedNodes) {
            System.out.println("   " + installedNode);
        }
    }
}
